package com.example.english.fragment;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.SearchView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.english.App;
import com.example.english.R;
import com.example.english.adapter.Tab2Adapter;
import com.example.english.db.WordBean;

import java.util.ArrayList;
import java.util.List;

public class Tab2Fragment extends Fragment {

    private RecyclerView recyclerView;
    private SearchView searchView;
    private Tab2Adapter adapter;
    private TextView tv_add ;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_tab2,container,false);
        return view;

    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        tv_add = view.findViewById(R.id.tv_add);
        recyclerView = view.findViewById(R.id.recyclerView);
        searchView = view.findViewById(R.id.searchView);

        searchView.setIconified(false);
        searchView.setSubmitButtonEnabled(true);
        adapter = new Tab2Adapter();
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

        List<WordBean> list = App.dbsqLite.getListWord();
        if(list != null){
            adapter.setList(list);
        }

        tv_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                View view1 = LayoutInflater.from(view.getContext()).inflate(R.layout.dialog_add,null);
                EditText et_english = view1.findViewById(R.id.et_english);
                EditText et_translate = view1.findViewById(R.id.et_translate);

                AlertDialog alertDialog =  new AlertDialog.Builder(view.getContext())
                        .setTitle("添加单词")
                        .setView(view1)
                        .setNegativeButton("新增", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                String en = et_english.getText().toString();
                                String tr = et_translate.getText().toString();
                                if(TextUtils.isEmpty(en) || TextUtils.isEmpty(tr)){
                                    Toast.makeText(view.getContext() ,"请输入内容",Toast.LENGTH_LONG).show();
                                    return;
                                }
                                WordBean bean = new WordBean();
                                bean.setEnglish(en);
                                bean.setTranslate(tr);
                                App.dbsqLite.add(bean);
                                adapter.add(bean);
                                dialogInterface.dismiss();
                            }
                        })
                        .setPositiveButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                dialogInterface.dismiss();
                            }
                        })
                        .create();
                alertDialog.show();
            }
        });

//        searchView.setOnSearchClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//
//            }
//        });

        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String s) {
                Log.d("setOnQueryTextListener","onQueryTextSubmit");
                List<WordBean> beans = new ArrayList<>();
                for (int i = 0 ; i < adapter.getList().size(); i++){
                    if(adapter.getList().get(i).getEnglish().contains(s) || adapter.getList().get(i).getTranslate().contains(s)){
                        beans.add(adapter.getList().get(i));
                    }
                }
                adapter.setList(beans);
                return true;
            }

            @Override
            public boolean onQueryTextChange(String s) {
                Log.d("setOnQueryTextListener","onQueryTextChange");
                if(TextUtils.isEmpty(s)){
                    List<WordBean> list = App.dbsqLite.getListWord();
                    if(list != null){
                        adapter.setList(list);
                    }
                }
                return true;
            }
        });

    }
}
